<script setup>
	import {
		computed,
		getCurrentInstance,
	} from "vue";
	import {
		useStore
	} from "vuex";

	import {
		onShow
	} from "@dcloudio/uni-app"; // 正确导入 onLoad

	const {
		proxy
	} = getCurrentInstance();
	const store = useStore();
	const userInfo = computed(() => store.state.userInfo);

	/**
	 * 设置用户信息
	 */
	const setUserInfoFromCache = () => {
		if (userInfo.value == null) {
			proxy.$tool.setUserInfoToVuex(store, null);
		}
	};


	/**
	 * 捕获第一次进入小程序
	 */
	onShow(() => {
		const pages = getCurrentPages();
		//获取当前页面
		const currentPage = pages[pages.length - 1];
		//设置用户缓存
		setUserInfoFromCache();
		//判断是否存在登录用户
		//获取用户最近的房间
		proxy.$request.get("/bus-room-user/findLoginUserLatelyOpenRoom").then(res => {
			if (res != null) {
				//判断房间状态
				if (res.isStart === 0) {
					//房间未开始
					//判断当前页面是否是这个页面
					if (currentPage == null || currentPage.route.indexOf("/joinRoom/joinRoom") === -1) {
						uni.navigateTo({
							url: `/pages/joinRoom/joinRoom?id=${res.roomId}`
						})
					}
				}
				if (res.isStart === 1) {
					//房间已经开始
					if (currentPage == null || currentPage.route.indexOf("/scoring/scoring") === -1) {
						uni.navigateTo({
							url: `/pages/scoring/scoring?id=${res.roomId}`
						})
					}

				}
			}
		}).catch(() => {

		})
	})
</script>

<style lang="scss">
	/*每个页面公共css */
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "@/uni_modules/uview-plus/index.scss";
</style>